package com.ruoyi.hotel.controller;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.hotel.domain.HmCheckin;
import com.ruoyi.hotel.domain.HmServiceRequest;
import com.ruoyi.hotel.domain.HmSupply;
import com.ruoyi.hotel.domain.HmTask;
import com.ruoyi.hotel.domain.bo.HmServiceRequestBo;
import com.ruoyi.hotel.domain.bo.HmSupplyBo;
import com.ruoyi.hotel.domain.bo.HmTaskBo;
import com.ruoyi.hotel.domain.bo.HmWorkRateBo;
import com.ruoyi.hotel.domain.vo.HmTaskVo;
import com.ruoyi.hotel.mapper.HmCheckinMapper;
import com.ruoyi.hotel.mapper.HmServiceRequestMapper;
import com.ruoyi.hotel.mapper.HmSupplyMapper;
import com.ruoyi.hotel.mapper.HmTaskMapper;
import com.ruoyi.hotel.service.IHmCheckinService;
import com.ruoyi.hotel.service.IHmServiceRequestService;
import com.ruoyi.hotel.service.IHmSupplyService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.service.IHmTaskService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分配，任务查看，任务反馈完成情况
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/task")
public class HmTaskController extends BaseController {

    private final IHmTaskService iHmTaskService;
    private final ISysUserService userService;
    private final IHmServiceRequestService iHmServiceRequestService;
    private final IHmSupplyService iHmSupplyService;

    /**
     * 查询任务列表
     */
    @SaCheckPermission("hotel:task:list")
    @GetMapping("/list")
    public TableDataInfo<HmTaskVo> list(HmTaskBo bo, PageQuery pageQuery) {
        return iHmTaskService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取任务详细信息
     *
     * @param taskId 主键
     */
    @SaCheckPermission("hotel:task:query")
    @GetMapping("/{taskId}")
    public R<HmTaskVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long taskId) {
        return R.ok(iHmTaskService.queryById(taskId));
    }

    /**
     * 新增任务
     */
    @SaCheckPermission("hotel:task:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmTaskBo bo) {
        return toAjax(iHmTaskService.insertByBo(bo));
    }

    /**
     * 修改任务
     */
    @SaCheckPermission("hotel:task:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmTaskBo bo) {
        return toAjax(iHmTaskService.updateByBo(bo));
    }

    /**
     * 删除任务
     *
     * @param taskIds 主键串
     */
    @SaCheckPermission("hotel:task:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] taskIds) {
        return toAjax(iHmTaskService.deleteWithValidByIds(Arrays.asList(taskIds), true));
    }


    /**
     * 获取个人任务
     * @param hmTask
     * @return TableDataInfo<HmTaskVo>
     */
    @GetMapping("/getMyTask")
    public TableDataInfo<HmTaskVo> getMyTask(HmTaskBo hmTask){
        Long id=getUserId();
        hmTask.setWorkerId(id);
        return TableDataInfo.build(iHmTaskService.queryList(hmTask));
    }

    /**
     * 获取个人发送的任务
     * @param hmTask
     * @return TableDataInfo<HmTaskVo>
     */
    @GetMapping("/getMyTaskSent")
    public TableDataInfo<HmTaskVo> getMyTaskSent( HmTaskBo hmTask){
        Long id=getUserId();
        hmTask.setSenderId(id);
        return TableDataInfo.build(iHmTaskService.queryList(hmTask));
    }

    /**
     * 发送任务
     * @param task
     */
    @PostMapping("/sendTask")
    public R<Void> sendTask(@RequestBody HmTaskBo task){
        Long id=getUserId();

        task.setSenderId(id);
        task.setIsDone("n");

        //有物资使用
        if(task.getSupplyId()!=null){
            HmSupply hmSupply=iHmSupplyService.selectById(task.getSupplyId());
            //没有足够物资
            if(hmSupply.getSupplyAmount()<task.getSupplyAmount()){
                return toAjax(false);
            }
            hmSupply.setSupplyAmount(hmSupply.getSupplyAmount()-task.getSupplyAmount());
            iHmSupplyService.updateByEntity(hmSupply);
        }

        iHmTaskService.insertByBo(task);

        //更新服务需求
        HmServiceRequestBo hmServiceRequest=new HmServiceRequestBo();
        hmServiceRequest.setServiceRequestId(task.getServiceRequestId());
        hmServiceRequest.setIsDone("g");
        iHmServiceRequestService.updateByBo(hmServiceRequest);

        return  toAjax(true);

    }

    /**
     * 查服务员
     * @return TableDataInfo<SysUser>
     */

    @GetMapping("/getWorker")
    public TableDataInfo<SysUser> getWorker() {
        SysUser user=new SysUser();
        user.setDeptId(105L);
        return TableDataInfo.build(userService.selectUserList(user));
    }


    /**
     * 查物资
     * @return TableDataInfo<HmSupply>
     */
    @GetMapping("/getSupply")
    public TableDataInfo<HmSupply> getSupply() {

        QueryWrapper<HmSupply> hmSupplyQueryWrapper=new QueryWrapper<>();
        hmSupplyQueryWrapper.gt("supply_amount",0);

        return TableDataInfo.build(iHmSupplyService.selectList(hmSupplyQueryWrapper));
    }


    /**
     * 只查询正在等待处理的服务需求(用来生成任务)
     */
    @GetMapping("/getListNow")
    public TableDataInfo<HmServiceRequest> getListNow() {
        return TableDataInfo.build(iHmServiceRequestService.getWaitingServiceRequests());
    }

    /**
     * 取消任务
     * @param task
     */
    @PostMapping("/taskCancel")
    public void taskCancel(@RequestBody HmTaskBo task){
        task.setIsDone("q");
        iHmTaskService.updateByBo(task);
    }

    /**
     * 服务员反馈任务完成
     * @param task
     */
    @PostMapping("/taskDone")
    public void taskDone(@RequestBody HmTask task){
        task.setIsDone("y");
        iHmTaskService.updateById(task);
    }

}
